package com.alm.token.base;

import android.content.Context;
import android.support.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by lidengzhou on 17/6/12.
 */

public abstract class BaseAdapter<T extends BaseAdapter.BaseItem> extends BaseQuickAdapter<T, BaseViewHolder> {

    private Set<Integer> layouts;
    protected Context context;

    public BaseAdapter(@NonNull Context context) {
        super(null);

        this.context = context;
        layouts = new HashSet<>();

        setMultiTypeDelegate(new MultiTypeDelegate<T>() {
            @Override
            protected int getItemType(T t) {
                if (t.getLayoutId() == 0) {
                    throw new IllegalStateException("LayoutId is null!");
                }

                if (!layouts.contains(t.getLayoutId())) {

                    layouts.add(t.getLayoutId());

                    BaseAdapter.this.getMultiTypeDelegate()
                            .registerItemType(t.getLayoutId(), t.getLayoutId());

                }

                return t.getLayoutId();
            }
        });
    }

    @Override
    protected final void convert(BaseViewHolder helper, T item) {
        convert(helper, item, helper.getItemViewType());
    }

    protected abstract void convert(BaseViewHolder helper, T item, int itemType);

    public static class BaseItem implements Serializable {

        protected int layoutId;

        public int getLayoutId() {
            return layoutId;
        }

        public void setLayoutId(int layoutId) {
            this.layoutId = layoutId;
        }
    }
}
